package fire.and.near.challenger.fragments;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import fire.and.near.challenger.R;
import fire.and.near.challenger.camera.CameraHelper;
import fire.and.near.challenger.database.TableConstants.ChallengesTable;
import fire.and.near.challenger.database.TableConstants.FriendsTable;
import fire.and.near.challenger.util.Consts;

public class ChallengeFriendFragment extends Fragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View v = inflater.inflate(R.layout.challenge_friend_fragment,
				container, false);

		Spinner spinner = (Spinner) v.findViewById(R.id.spinner_challengeType);
		EditText description = (EditText) v
				.findViewById(R.id.editText_description);
		description.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if (s.length() >= Consts.MESSAGE_MAX_LENGTH) {
					Toast.makeText(
							v.getContext(),
							"Challenge description can't be longer than " + Consts.MESSAGE_MAX_LENGTH + " letters",
							Toast.LENGTH_LONG).show();
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});

		Button btnFriend = (Button) v.findViewById(R.id.button_friendPicker);
		ImageView imgView = (ImageView) v.findViewById(R.id.imageView_photo);

		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this.getActivity(), R.array.challenge_types,
				R.layout.spinner_item);
		adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
		spinner.setAdapter(adapter);

		Bundle args = getArguments();
		if (args != null) {
			int position = args.getInt(ChallengesTable.CHALLENGE_TYPE, 0);
			String desc = args.getString(ChallengesTable.DESCRIPTION);
			String friendName = args.getString(FriendsTable.NAME);
			String photoPath = args.getString(ChallengesTable.PHOTO_PATH);

			spinner.setSelection(position);

			description.setText(desc);

			if (friendName != null) {
				btnFriend.setText(friendName);
			}

			if (photoPath != null) {
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inJustDecodeBounds = true;
				
				BitmapFactory.decodeFile(photoPath,
						options);
				
				int width = imgView.getWidth();
				int height = imgView.getHeight();
				
				options = CameraHelper
							.makeSampleOptions(getActivity(), options, width, height);
				
				options.inJustDecodeBounds = false;
				
				Bitmap bitmap = BitmapFactory.decodeFile(photoPath,
						options);
				
				imgView.setImageBitmap(bitmap);
			} else {
				imgView.setImageDrawable(getResources().getDrawable(
						R.drawable.camera_placeholder));
			}
		} else {
			imgView.setImageDrawable(getResources().getDrawable(
					R.drawable.camera_placeholder));
		}

		return v;
	}
}
